from django.shortcuts import render
from rest_framework.generics import ListAPIView,CreateAPIView,UpdateAPIView,DestroyAPIView
from rest_framework_extensions.cache.mixins import CacheResponseMixin
# Create your views here.
from areas.serializers import AreaSerializer,AddressSerializier
from areas.models import Area
from users.models import Address
from rest_framework.response import Response



class AreasView(CacheResponseMixin,ListAPIView):
    # 获取省的数据
    serializer_class = AreaSerializer
    queryset = Area.objects.filter(parent=None)  # 查询所有省的信息


class AreaView(CacheResponseMixin,ListAPIView):
    # 获取区县信息
    serializer_class = AreaSerializer
    # queryset = Area.objects.filter(parent=pk)  # 查询所有省的信息
    # 重写方法
    def get_queryset(self):

        pk = self.kwargs['pk']
        return Area.objects.filter(parent_id=pk)



class AddressView(ListAPIView,CreateAPIView,UpdateAPIView,DestroyAPIView):
    serializer_class = AddressSerializier
    queryset = Address.objects.filter(is_deleted=False)


    def delete(self, request, *args, **kwargs):
        address = self.get_object()
        address.is_deleted = True
        address.save()

        return Response()


